package com.dj.shop.service.impl;

import com.dj.shop.bean.CartGoods;
import com.dj.shop.mapper.GoodsMapper;
import com.dj.shop.mapper.ShoppingCartMapper;
import com.dj.shop.model.Goods;
import com.dj.shop.model.ShoppingCart;
import com.dj.shop.service.ShoppingCartService;
import com.dj.shop.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author renshp
 * @Title: ShoppingCartServiceImpl
 * @Description: TODO
 * @date 2019/3/6
 */
@Service("shoppingCartService")
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Autowired
    GoodsMapper goodsMapper;


    @Override
    public int addGoods(int userId, int shopId, int shopGoodsId) {

        ShoppingCart shoppingCart = shoppingCartMapper.selectByPars(userId, shopId, shopGoodsId);

        if (shoppingCart == null){
            // 新增记录
            shoppingCart = new ShoppingCart();
            shoppingCart.setUserId(userId);
            shoppingCart.setShopId(shopId);
            shoppingCart.setShopGoodsId(shopGoodsId);
            shoppingCart.setGoodsNumber(1);
            shoppingCart.setStatus("1");    // 默认为1-正常
            shoppingCart.setCreateDatetime(DateUtil.getNowDate());
            return shoppingCartMapper.insert(shoppingCart);
        }else{
            // 原纪录数量+1
            shoppingCart.setGoodsNumber(shoppingCart.getGoodsNumber() + 1);
            return shoppingCartMapper.updateByPrimaryKeySelective(shoppingCart);
        }

    }

    @Override
    public int deleteGoods(int userId, int shopId, int shopGoodsId) {

        ShoppingCart shoppingCart = shoppingCartMapper.selectByPars(userId, shopId, shopGoodsId);
        if (shoppingCart == null){
            return 0;
        }

        if (shoppingCart.getGoodsNumber() > 1){
            shoppingCart.setGoodsNumber(shoppingCart.getGoodsNumber() - 1);
            return shoppingCartMapper.updateByPrimaryKeySelective(shoppingCart);
        }else{
            return shoppingCartMapper.deleteByPrimaryKey(shoppingCart.getCartGoodsId());
        }

    }

    @Override
    public List<CartGoods> queryAllGoods(int userId, int shopId) {

        List<CartGoods> cartGoodsList = new ArrayList<CartGoods>();

        Map parMap = new HashMap();
        parMap.put("userId",userId);
        parMap.put("shopId",shopId);

        List<ShoppingCart> shoppingCartList = shoppingCartMapper.queryAllGoods(parMap);
        for (int i=0; i<shoppingCartList.size(); i++){

            ShoppingCart shoppingCart = shoppingCartList.get(i);

            CartGoods cartGoods = new CartGoods();
            cartGoods.setCartGoodsId(shoppingCart.getCartGoodsId());
            cartGoods.setShopGoodsId(shoppingCart.getShopGoodsId());
            cartGoods.setGoodsId(shoppingCart.getGoodsId());
            cartGoods.setGoodsNumber(shoppingCart.getGoodsNumber());

            Goods goods = goodsMapper.selectByPrimaryKey(shoppingCart.getGoodsId());
            if (goods != null){
                cartGoods.setGoodsTitle(goods.getGoodsTitle());
                cartGoods.setGoodsSubtitle(goods.getGoodsSubtitle());
                cartGoods.setGoodsIcon(goods.getGoodsIcon());
                cartGoods.setGoodsPrice(BigDecimal.valueOf(10.32));
            }
            cartGoodsList.add(cartGoods);
        }

        return cartGoodsList;
    }

    @Override
    public int clearAllGoods(int userId, int shopId) {
        Map parMap = new HashMap();
        parMap.put("userId",userId);
        parMap.put("shopId",shopId);
        return shoppingCartMapper.clearAllGoods(parMap);
    }

    @Override
    public int batchGoods(int userId, int shopId, String status, List<Integer> cartGoodsIdList) {
        return 0;
    }
}
